package struct;

/**
 * BinaryTree
 */
public class BinaryTree {


    public BinaryTree(){}

    private TreeNode root;

    public TreeNode getRoot(){
        return root;
    }

    public void add(Integer data){
        if(root == null){
            root = new TreeNode();
            root.setLevel(0);
            root.setData(data);
        }else {
            cycle(root, data);
        }
    }

    public void cycle(TreeNode node,Integer data){
        if(node.getData() > data){
            if (node.getLeft() == null) {
                TreeNode left = new TreeNode();
                left.setData(data);
                left.setLevel(node.getLevel() + 1);
                node.setLeft(left);
            }else{
                cycle(node.getLeft(), data);
            }
        }else {
            if (node.getRight() == null) {
                TreeNode right = new TreeNode();
                right.setData(data);
                right.setLevel(node.getLevel() + 1);
                node.setRight(right);
            }else {
                cycle(node, data);
            }
        }
    }




	//先序遍历
	public void prevOrder(TreeNode node){
		if(node == null){
			return;
		}
		System.out.print(node.getData()+",");
		prevOrder(node.getLeft());
		prevOrder(node.getRight());
    }


    /**
     * InnerBinaryTree
     */
    class TreeNode {

        /**
         * 节点所在的层
         */
        private Integer level;
        /**
         * 数据
         */
        private Integer data;
        /**
         * 左节点
         */
        private TreeNode left;
        /**
         * 右节点
         */
        private TreeNode right;

        /**
         * @return the level
         */
        public Integer getLevel() {
            return level;
        }

        /**
         * @param level the level to set
         */
        public void setLevel(Integer level) {
            this.level = level;
        }

        /**
         * @return the data
         */
        public Integer getData() {
            return data;
        }

        /**
         * @param data the data to set
         */
        public void setData(Integer data) {
            this.data = data;
        }

        /**
         * @return the left
         */
        public TreeNode getLeft() {
            return left;
        }

        /**
         * @param left the left to set
         */
        public void setLeft(TreeNode left) {
            this.left = left;
        }

        /**
         * @return the right
         */
        public TreeNode getRight() {
            return right;
        }

        /**
         * @param right the right to set
         */
        public void setRight(TreeNode right) {
            this.right = right;
        }
    }


    public static void main(String[] args) {
        BinaryTree tree = new BinaryTree();
        tree.add(1);
        tree.add(10);
        tree.add(5);
        tree.add(7);
        tree.add(6);

        tree.prevOrder(tree.getRoot());
    }

}